package com.naizhuai.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.naizhuai.AppContext;
import com.naizhuai.Model.AccountHelper;
import com.naizhuai.Model.ModelProvider;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.Model.db.user.UserDataSourse;
import com.naizhuai.Model.db.user.UserRepository;
import com.naizhuai.R;
import com.naizhuai.network.NetWorkObserver;
import com.naizhuai.network.apistore.CommonManagerApiStore;
import com.naizhuai.network.apistore.apistoreiml.CommonManagerIml;
import com.naizhuai.ui.entity.StartUp;
import com.naizhuai.ui.entity.management.CompanyBean;
import com.naizhuai.ui.user.login.LoginActivity;
import com.naizhuai.util.DownLoadConstants;
import com.naizhuai.util.command.RemindReceiver;
import com.tencent.bugly.beta.Beta;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SplashActivity extends BaseActivity {
    private UserRepository mUserRepository;
    @BindView(R.id.image)
    ImageView image;

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        mUserRepository = ModelProvider.provideUserRepository();
        initStartUp();
    }

    private void initStartUp() {
        image.postDelayed(new Runnable() {
            @Override
            public void run() {
                updateData();
            }

        }, 1500);
        RemindReceiver.startReceiver(this,null);

        CommonManagerIml.getInstance().getStartUp()
                .subscribe(new NetWorkObserver<StartUp>() {
                    @Override
                    protected void onFail(String errormsg) {

                    }
                    @Override
                    protected void onSuccess(StartUp result) {
                        getSp().saveValue(PersonUserBean.USER_TASK_SEE_IMAGE, result.resultKeyImage);
                        List<StartUp.ThankCardImageBean> cardList = result.thanksCard;
                        for (int index = 0; index < cardList.size(); index++) {
                            setShareValue(StartUp.THANK_CARD_SELECTED_IMG + index, cardList.get(index).selectedImageUrl);
                            setShareValue(StartUp.THANK_CARD_SHOW_IMG + index, cardList.get(index).showImageUrl);
                        }
                        setShareValue(StartUp.THANK_CARD_NUMBER, cardList.size());
                        setShareValue(DownLoadConstants.DOWNLOAD_APK_URL_KEY, result.appVersion.upConfig.downloadUrl);
                    }
                });
    }

    private void login() {
        LoginActivity.start(this);
        finish();
    }

    private void updateData() {
        if (!TextUtils.isEmpty(AccountHelper.getUid())) {
            mUserRepository.updateLoginUser(new UserDataSourse.GetUserCallback() {
                @Override
                public void onUserLoaded(PersonUserBean user) {
                    login();
                }
                @Override
                public void onDataNotAvailable() {
                }
            });
        } else {
            login();
        }
    }
}
